<template>
  <el-card>
    <template #header>
      <h3>商品分类</h3>
    </template>
    <div>
      <el-button size="mini" type="primary" @click="dialogVisible = true"
        >添加分类</el-button
      >
      <!-- 添加商品分类的面板 -->
      <el-dialog title="添加分类" :visible.sync="dialogVisible" width="42%">
        <el-form
          :model="cateForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-cateForm"
          size="mini"
        >
          <el-form-item label="分类名称" prop="name">
            <el-input v-model="cateForm.name"></el-input>
          </el-form-item>
          <el-form-item label="是否启用" prop="state">
            <el-switch
              v-model="cateForm.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" size="mini">取消</el-button>
          <el-button type="primary" size="mini" @click="addCate('ruleForm')"
            >确定</el-button
          >
        </span>
      </el-dialog>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%" size="mini">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column label="分类名称">
          <!-- 改变输入框的状态 -->
          <template v-slot="{ row }">
            <span v-if="row.flag">{{ row.cateName }}</span>
            <el-input v-else v-model="row.cateName" size="mini"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="是否启用">
          <template v-slot="{ row }">
            <el-switch
              v-model="row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="row.flag"
            >
              >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              size="mini"
              :type="row.flag ? 'primary' : 'success'"
              @click="editCate(row)"
              >{{ row.flag ? '编辑' : '完成' }}</el-button
            >
            <el-button size="mini" type="danger" @click="del(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-card>
</template>
<script>
import {
  goodsCate,
  addGoodsCate,
  delGoodsCate,
  editCate
} from '../../api/goods.js'

export default {
  data() {
    return {
      cateForm: {
        name: '',
        state: true
      },
      rules: {
        name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      dialogVisible: false,
      pageSize: 5,
      currentPage: 1,
      total: 1,
      tableData: []
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getGoodsList()
    },
    // 获取商品分类
    async getGoodsList() {
      const { data, total } = await goodsCate({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      // 给每一行绑定属性flag 控制开关的状态
      data.forEach(item => {
        item.flag = true
      })
      // console.log(data)
      this.tableData = data
      this.total = total
    },
    // 添加商品分类
    addCate(str) {
      this.$refs[str].validate(async valid => {
        if (valid) {
          const { code, msg } = await addGoodsCate({
            cateName: this.cateForm.name,
            state: this.cateForm.state
          })
          // console.log(code, msg)
          if (code === 0) {
            this.getGoodsList()
            this.dialogVisible = false
            this.$message({
              type: 'success',
              message: msg
            })
          }
        } else {
          this.$message.error('请正确填写信息')
        }
      })
    },
    // 删除商品分类
    del(id) {
      this.$confirm('此操作将永久删除这条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await delGoodsCate({ id })
          this.getGoodsList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑商品名
    async editCate(row) {
      if (row.flag) {
        row.flag = !row.flag
      } else {
        const { code, msg } = await editCate({
          id: row.id,
          cateName: row.cateName,
          state: Boolean(row.state)
        })
        // console.log(code, msg)
        // 做出判断code=0 修改成功
        if (code === 0) {
          this.$message({
            type: 'success',
            message: msg
          })
          this.getGoodsList()
        } else {
        }
        row.flag = !row.flag
      }
    }
  },
  watch: {
    dialogVisible() {
      if (!this.dialogVisible) {
        this.$refs.ruleForm.resetFields()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0px;
}
</style>
